package com.webvihar.access2db.activities;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Window;
import android.view.WindowManager;

import com.webvihar.access2db.R;
import com.webvihar.access2db.model.Authentication;
import com.webvihar.common.db.DbService;

public class SplashActivity extends BaseActivity {

	private DbService db;
	public long SPLASH_DISPLAY_DURATION = 3000;
	public long SPLASH_DISPLAY_INTERVAL = 1000;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.splash);
		initDbService();
		new CountDownTimer(SPLASH_DISPLAY_DURATION, SPLASH_DISPLAY_INTERVAL) {
			@Override
			public void onFinish() {
				showNextScreen();
			}

			@Override
			public void onTick(long millisUntilFinished) {
			}
		}.start();
	}

	private void initDbService() {
		db = new DbService(this);
	}
	
	private void showNextScreen() {
		// TODO: condition should be modified and tested
		// set preferences if its first time
		List<Authentication> authList = db.Get(Authentication.class); 
		if(authList == null || authList.size() <=0) {
			Intent homeIntent = new Intent(getApplicationContext(), RegisterActivity.class);
			startActivity(homeIntent);
		} else {
			String securityCode = authList.get(0).securityCode;
			Intent homeIntent = new Intent(getApplicationContext(), SecurityCheckActivity.class);
			homeIntent.putExtra(KEY_EMAIL_ID, authList.get(0).emailId);
			homeIntent.putExtra(KEY_SECURITY_CODE, securityCode);
			startActivity(homeIntent);
		}
		
		finish();
	}
}
